package com.dream52.pms.bst.controller;

import com.dream52.pms.base.BaseController;
import com.dream52.pms.bst.entity.BstHotel;
import com.dream52.pms.bst.service.BstHotelService;
import com.dream52.pms.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/bst/bstHotel")
public class BstHotelController extends BaseController {
    @Autowired
    private BstHotelService bstHotelService;
    @GetMapping("/list")
    @ResponseBody
    public String getBstHotelList(Integer pageNo,Integer pageSize) throws Exception{
        SysUser user = this.getUserSession();
        String where = "1 = 1";
        if(user != null){
            if(null==pageNo||pageNo.toString().length() < 1){
                List bstHotelList = this.bstHotelService.getListByWhere(where,"id ASC");
                this.result.put("bstHotelList", bstHotelList);
            }else{
                PageInfo pageInfo = this.bstHotelService.getPageByWhere(where,pageNo, null==pageSize?this.pageSize:pageSize,"id ASC");
                this.result.put("pageInfo", pageInfo);
            }
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE,"请先登录");
    }
    @GetMapping("/get")
    @ResponseBody
    public String getBstHotel(Integer id) throws Exception{
        SysUser user = this.getUserSession();
        BstHotel b = new BstHotel();
        if(user != null ){
            if(id!=null && id>0){
                b = this.bstHotelService.getById(id);
            }
            this.result.put("bean", b);
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE,"请先登录");
    }
    @PostMapping("/save")
    @ResponseBody
    public String saveBstHotel(BstHotel entity) throws Exception{
        SysUser user = this.getUserSession();
        if (entity.getId() == null){
            this.bstHotelService.save(entity);
        }else {
            this.bstHotelService.update(entity);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }
    @PostMapping("/del")
    @ResponseBody
    public String delBstHotel( Integer id) throws Exception{
        SysUser user = this.getUserSession();
        if (id != null&&id.toString().length()>0){
            this.bstHotelService.deleteById(id);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }
}